package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.constant.LikeContext;
import com.honglv.context.BaseContext;
import com.honglv.mapper.RounteMapper;
import com.honglv.pojo.dto.GetRounteDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateRounteDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.*;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadRounteDTO;

import com.honglv.pojo.entity.Rounte;
import com.honglv.pojo.vo.GetRountesByCityVO;
import com.honglv.service.RounteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RounteServiceImpl implements RounteService {
    @Autowired
    private RounteMapper rounteMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 用户上传路线
     * @param uploadRounteDTO
     * @return
     */
    @Override
    public void upload(UploadRounteDTO uploadRounteDTO) {
        Long authorId = BaseContext.getCurrentId();
        Rounte rounte=new Rounte();
        BeanUtils.copyProperties(uploadRounteDTO,rounte);
        rounte.setAuthorId(authorId);
        rounte.setLike(0L);
        rounte.setViews(0L);
        rounteMapper.insert(rounte);
    }
    /**
     * 根据城市分页获取路线
     * @param
     * @return
     */
    @Override
    public PageVO<GetRountesByCityVO> getRountesByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Rounte> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Rounte> list = rounteMapper.selectList(qw);
        GetRountesByCityVO gf=new GetRountesByCityVO();
        List<GetRountesByCityVO> rountes=new ArrayList<>();
        for(Rounte rounte:list){
            BeanUtils.copyProperties(rounte,gf);
            rountes.add(gf);
        }
        if (rountes == null){
            return null;
        }
        PageInfo<GetRountesByCityVO> pageInfo = new PageInfo<>(rountes);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * Description:获取点赞量前四的路线
     * date: 2024/03/31
     * author:王铭杰
     */
    public List<RounteShowVO> getTopRounte() {
        List<RounteShowVO> list = rounteMapper.getTopRounte();
        return list;
    }

    /**
     * Description:分页获取路线
     * date: 2024/03/31
     * author:王铭杰
     */
    @Override
    public PageVO<RounteShowVO> getAllRountes(PageDTO pageDTO) {
        System.out.println(pageDTO.getPageSize());
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getPageSize());
        List<RounteShowVO> list = rounteMapper.getAllRountes();
        if (list == null) {
            return null;
        }
        PageInfo<RounteShowVO> pageInfo = new PageInfo<>(list);
        return new PageVO<RounteShowVO>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * Description:获取路线详情页
     * date: 2024/03/31
     * author:王铭杰
     */
    @Override
    public GetRounteVO getRounte(GetRounteDTO rounteDTO) {
//        public static final String[] likeStatus = {"rounte","attractions","strategy","news"};
        GetRounteVO rounteVO = rounteMapper.getRounte(rounteDTO.getId());
        if(rounteVO == null){
            return null;
        }
        String key = LikeContext.likeStatus[0] + rounteDTO.getId();
        Long userId = BaseContext.getCurrentId();
        boolean identify = Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(key, String.valueOf(userId)));
        Integer likeStatus = identify? 1 : 0;
        rounteVO.setLikeStatus(likeStatus);
        return rounteVO;
    }

    /**
     * Description:通过用户id获取路线
     * date: 2024/03/31
     * author:王铭杰
     */
    @Override
    public Result<List<RounteShowVO>> getRountesByAuthorId(Long userId) {
        List<RounteShowVO> list = rounteMapper.getRountesByAuthorId(userId);
        return (Result<List<RounteShowVO>>) list;
    }

    @Override
    public Result deleteRounte(Long id) {
        rounteMapper.deleteRounte(id);
        return Result.success("删除成功！");
    }

    @Override
    public Result updateRountel(UpdateRounteDTO updateRounteDTO) {
        if(rounteMapper.updateRounte(updateRounteDTO) > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败，未知错误！");
    }

}


